package com.example.sale.Controller;
import com.example.sale.True.Verification;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@CrossOrigin
@RequestMapping
public class CaptchaController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/captcha")
    public void captcha(@RequestParam String key,HttpServletRequest request,HttpServletResponse response) throws IOException{
        //验证码的长宽以及长度
        SpecCaptcha captcha = new SpecCaptcha(135,33,5);
        captcha.setCharType(captcha.TYPE_NUM_AND_UPPER);
        //首先把验证码在后台保存一份，但是不存在session当中，存在redis里面，也可以存在后台的某一个map里面
        Verification verification = new Verification();
        verification.setKey(key);
        verification.setVscode(captcha.text());
//        rabbitTemplate.convertAndSend("hmall.direct","yellow",verification);
        //下面是测试注释必要的时候可以放开
        stringRedisTemplate.opsForValue().set(key,captcha.text());
        //设置登录的验证码的
//        stringRedisTemplate.opsForValue().get(key);
//        System.out.println(stringRedisTemplate.opsForValue().get("verCode"));
//        System.out.println("key="+key);
        CaptchaUtil.out(captcha,request,response);
    }
}
